package com.duowan.realtime.common.base;


import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;

import javax.sql.DataSource;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.util.ResourceUtils;

import com.duowan.common.test.dbunit.DBUnitFlatXmlHelper;

/**
 * 本基类主要为子类指定好要装载的spring配置文件
 * 及在运行测试前通过dbunit插入测试数据在数据库中,运行完测试删除测试数据 
 *
 * @author badqiu
 * 请设置好要装载的spring配置文件,一般开发数据库与测试数据库分开
 * 所以你要装载的资源文件应改为"classpath:/spring/*-test-resource.xml"
 */
@ContextConfiguration(locations={"classpath:/spring/*-resource.xml",
								 "classpath:/spring/*-validator.xml",
                                 "classpath:/spring/*-datasource.xml",
                                 "classpath:/spring/*-dao.xml"
})
@TestExecutionListeners() 
public class BaseDaoTestCase extends AbstractTransactionalJUnit4SpringContextTests  {
	private static Logger logger = LoggerFactory.getLogger(BaseDaoTestCase.class) ; 
	protected DBUnitFlatXmlHelper dbUnitHelper = new DBUnitFlatXmlHelper();
	protected DataSource dataSource;
	protected DataSource getDataSource() {
		return dataSource;
	}
	
	@Autowired(required=true)
	@Qualifier("data-computingDataSource")
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
		logger.info("setDataSource:" + dataSource.toString());
		super.setDataSource(dataSource);
	}
	
	@Before
	public void onSetUpInsertTestDatas() throws Exception {
		String jdbcSchema = null;  // set schema for oracle,出现AmbiguousTableNameException时，使用命令:purge recyclebin清空一下oracle回收站
		dbUnitHelper.setDataSource(getDataSource(),jdbcSchema);
		initTables();
		dbUnitHelper.insertTestDatas(getDbUnitDataFiles());
	}
	
	private void initTables() throws Exception {
		for (String sqlFilePath : getDbUnitDataFiles("sql")) {
			String sql = FileUtils.readFileToString(new File(sqlFilePath)) ; 
			simpleJdbcTemplate.update(sql ) ;
		}
	}
	
	/** 得到要加载的dbunit文件 */
	protected String[] getDbUnitDataFiles() {
		return getDbUnitDataFiles("testdata");
	}
	
	
	/** 得到要加载的dbunit文件 */
	protected String[] getDbUnitDataFiles(String path) {
		File[] fileList = null;
		try {
			fileList = ResourceUtils.getFile("classpath:" + path).listFiles();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		String[] filePathList = new String[fileList.length];
		for (int i = 0; i < filePathList.length; i++) {
			filePathList[i] = fileList[i].getPath();
		}
		return filePathList;
	}
	
    protected void insertTestData(String classpathFileName) {
        try {
            dbUnitHelper.insertTestData(ResourceUtils.getFile("classpath:"+classpathFileName));
        }catch(Exception e) {
            throw new RuntimeException("insertTestData error,classpathFileName:"+classpathFileName,e);
        }
    }
	
    @Test
    public void testBaseDao(){
    	assertTrue(true);
    }
}
